﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace SheepClock.CustomUIComponents
{
    public partial class AlarmSettingsItem : UserControl
    {
        public AlarmSettingsItem()
        {
            InitializeComponent();
        }

        private void hourMinus_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PushStoryBoard_hourMinus.Begin();

            ChangeTextValue(hourTextBox, -1, 24);
        }

        private void hourPlus_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PushStoryBoard_hourPlus.Begin();

            ChangeTextValue(hourTextBox, 1, 24);
        }

        private void minuteMinus_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PushStoryBoard_minuteMinus.Begin();

            ChangeTextValue(minuteTextBox, -1, 60);
        }

        private void minutePlus_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PushStoryBoard_minutePlus.Begin();

            ChangeTextValue(minuteTextBox, 1, 60);
        }


        private void ChangeTextValue(TextBox textBox, int valueToAdd, int maxValue)
        {
            if (string.IsNullOrEmpty(textBox.Text))
                textBox.Text = "00";

            int textBoxValue;
            try
            {
                textBoxValue = Convert.ToInt32(textBox.Text);
            }
            catch (Exception ex)
            {
                textBoxValue = 0;
            }

            textBoxValue += valueToAdd;
            if (textBoxValue < 0)
                textBoxValue += maxValue;
            textBoxValue %= maxValue;

            textBox.Text = textBoxValue.ToString("00");
        }


        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            int textBoxValue = 0;
            try
            {
                textBoxValue = Convert.ToInt32((sender as TextBox).Text);
            }
            catch (Exception ex)
            {
                (sender as TextBox).Text = "00";
            }

            if (textBoxValue < 0)
                (sender as TextBox).Text = "00";
            else if (sender == hourTextBox && textBoxValue > 23)
                (sender as TextBox).Text = "00";
            else if (sender == minuteTextBox && textBoxValue > 59)
                (sender as TextBox).Text = "00";
            else
                (sender as TextBox).Text = textBoxValue.ToString("00");
            (sender as TextBox).SelectionStart = (sender as TextBox).Text.Length;
        }



        private void saveButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PushStoryBoard_saveButton.Begin();
        }

        private void deleteButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PushStoryBoard_deleteButton.Begin();
        }



        private void hourTextBox_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            //(sender as TextBox).SelectionStart = 0;
            //(sender as TextBox).SelectionLength = (sender as TextBox).Text.Length;
        }

    }
}